#include "process.h"

Process::Process (QObject* parent) : QProcess (parent) {

}

bool Process::startMSPowerPoint (const QString& filePath) {
    return (true);
}

#ifdef Q_WS_WIN
void taskkill (const QString& process) {
    QProcess taskkill;
    QStringList arg;
    arg.push_back ("/f");
    arg.push_back ("/im");
    arg.push_back (process);
    arg.push_back ("/t");
    QString taskkillPath = QProcessEnvironment::systemEnvironment ().value ("SystemRoot", "") + "/system32/taskkill.exe";
    taskkill.start(taskkillPath, arg);
    taskkill.waitForFinished ();
}
    void Process::terminate (void) {
        QProcess::terminate ();
        taskkill ("soffice.bin");
        taskkill ("powerpnt.exe");

    }
#endif // Q_WS_WIN

bool Process::startPresentation (const QString &filePath) {
    QStringList arg;
    arg.push_back (filePath);
    QString programFilesPath64 = QProcessEnvironment::systemEnvironment ().value ("ProgramFiles", "");
    QString programFilesPath86 = QProcessEnvironment::systemEnvironment ().value ("ProgramFiles(x86)", "");
    QString programPath;
    if (QFile::exists (programFilesPath64 + MSPOWERPNT14)) {
        arg.push_front (MSSHOW);
        programPath = programFilesPath64 + MSPOWERPNT14;
    }
    else {
        if (QFile::exists (programFilesPath86 + MSPOWERPNT14)) {
            arg.push_front (MSSHOW);
            programPath = programFilesPath86 + MSPOWERPNT14;
        }
        else {
            if (QFile::exists (programFilesPath64 + MSPOWERPNT12)) {
                arg.push_front (MSSHOW);
                programPath = programFilesPath64 + MSPOWERPNT12;
            }
            else {
                if (QFile::exists (programFilesPath86 + MSPOWERPNT12)) {
                    arg.push_front (MSSHOW);
                    programPath = programFilesPath86 + MSPOWERPNT12;
                }
                else {
                    if (QFile::exists (programFilesPath64 + MSPOWERPNT11)) {
                        arg.push_front (MSSHOW);
                        programPath = programFilesPath64 + MSPOWERPNT11;
                    }
                    else {
                        if (QFile::exists (programFilesPath86 + MSPOWERPNT11)) {
                            arg.push_front (MSSHOW);
                            programPath = programFilesPath86 + MSPOWERPNT11;
                        }
                        else {
                            if (QFile::exists (programFilesPath64 + MSPOWERPNT10)) {
                                arg.push_front (MSSHOW);
                                programPath = programFilesPath64 + MSPOWERPNT10;
                            }
                            else {
                                if (QFile::exists (programFilesPath86 + MSPOWERPNT10)) {
                                    arg.push_front (MSSHOW);
                                    programPath = programFilesPath86 + MSPOWERPNT10;
                                }
                                else {
                                    arg.push_front ("-nologo");
                                    arg.push_front ("-norestore");
                                    arg.push_front (SHOW);
                                    if (QFile::exists (programFilesPath64 + LOPOWERPNT)) {
                                        programPath = programFilesPath64 + LOPOWERPNT;
                                    }
                                    else {
                                        if (QFile::exists (programFilesPath86 + LOPOWERPNT)) {
                                            programPath = programFilesPath86 + LOPOWERPNT;
                                        }
                                        else {
                                            if (QFile::exists (programFilesPath64 + OOPOWERPNT)) {
                                                programPath = programFilesPath64 + OOPOWERPNT;
                                            }
                                            else {
                                                if (QFile::exists (programFilesPath86 + OOPOWERPNT)) {
                                                    programPath = programFilesPath86 + OOPOWERPNT;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    start (programPath, arg);
    waitForStarted ();
    if (state () == 0)
        return (false);
    return (true);
}
